//
//  BanBu_SnowView.m
//  Snow
//
//  Created by Wang Xj on 14-2-21.
//  Copyright (c) 2014年 BanBu. All rights reserved.
//

#import "BanBu_SnowView.h"
#define IMAGE_X                arc4random()%(int)__MainScreen_Height
#define IMAGE_ALPHA            ((float)(arc4random()%10))/10+0.3
#define IMAGE_WIDTH            arc4random()%16 + 24
#define PLUS_HEIGHT            Main_Screen_Height/25
#define Start_Y                -100
#import "UConstants.h"

@implementation BanBu_SnowView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
    }
    return self;
}

-(void)initWithImagePath:(NSString *)imagePath musicName:(NSString *)musicPath
{
    //设置图片和语音
    self.imageName = imagePath;
    NSLog(@"self.imageName:%@",self.imageName);
    
    self.musicName = musicPath;
    
    [self setup];
}

- (void)setup
{
//    self.backgroundColor = [UIColor colorWithRed:0.276 green:0.855 blue:1.000 alpha:1.000];
    self.backgroundColor = [UIColor clearColor];
    self.start = [UIButton buttonWithType:UIButtonTypeCustom];
    self.start.frame = CGRectMake(140,__MainScreen_Height-50 , 40, 30);
    [self.start setTitle:@"start" forState:UIControlStateNormal];
    [self.start addTarget:self action:@selector(statrOrStop:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:self.start];
    
	_imagesArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < 100; ++ i)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:PNGIMAGE2(self.imageName)];
        float x = IMAGE_WIDTH;
        imageView.frame = CGRectMake(IMAGE_X, Start_Y, x, x);
        imageView.alpha = IMAGE_ALPHA;
        [self addSubview:imageView];
        [_imagesArray addObject:imageView];
    }
}


static int i = 0;
- (void)makeSnow
{
    i = i + 1;
    if ([_imagesArray count] > 0)
    {
        UIImageView *imageView = [_imagesArray objectAtIndex:0];
        imageView.tag = i;
        [_imagesArray removeObjectAtIndex:0];
        [self snowFall:imageView];
    }
}

- (void)snowFall:(UIImageView *)aImageView
{
    [UIView beginAnimations:[NSString stringWithFormat:@"%i",aImageView.tag] context:nil];
    [UIView setAnimationDuration:6];
    [UIView setAnimationDelegate:self];
    
    int startX = round(random() % 320);
	int endX = round(random() % 320);
    
	aImageView.frame = CGRectMake(startX, Start_Y, aImageView.frame.size.width, aImageView.frame.size.height);
    
    aImageView.frame = CGRectMake(endX, __MainScreen_Height,aImageView.frame.size.width, aImageView.frame.size.height);
    
    [UIView commitAnimations];
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    UIImageView *imageView = (UIImageView *)[self viewWithTag:[animationID intValue]];
    float x = IMAGE_WIDTH;
    imageView.frame = CGRectMake(IMAGE_X, Start_Y, x, x);
    [_imagesArray addObject:imageView];
}

- (IBAction)statrOrStop:(UIButton *)sender
{
    if ([sender.currentTitle isEqualToString:@"start"])
    {
        if (self.audioPlayer == nil)
        {
            self.audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:self.musicName] error:nil];
        }
        self.audioPlayer.volume=0.5;
        self.audioPlayer.delegate = self;
        [self.audioPlayer prepareToPlay];
        [self.audioPlayer play];
        
        [sender setTitle:@"stop" forState:UIControlStateNormal];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:.15 target:self selector:@selector(makeSnow) userInfo:nil repeats:YES];
    }
    else
    {
        [sender setTitle:@"start" forState:UIControlStateNormal];
        [_timer invalidate];
        _timer = nil;
        _audioPlayer = nil;
        if ([_delegate respondsToSelector:@selector(didFinishPlayingSnows:)])
        {
            [_delegate didFinishPlayingSnows:self];
        }
    }
}

#pragma mark - AVAudioPlayerDelegate
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    [self statrOrStop:self.start];
}

@end
